package com.rwdesign.springboot.Waimai.controller;

import com.rwdesign.springboot.Waimai.service.UserService;
import com.rwdesign.springboot.Waimai.utils.ResultBody;
import com.rwdesign.springboot.Waimai.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * @author cdp
 * @create 2022-01-29 13:06
 */

//@CrossOrigin:跨域注解

@CrossOrigin
@RestController
@RequestMapping("/waimai")
public class UserController {

    @Autowired
    private UserService userService;

    public boolean isLogin(String userToken) {
        if (userToken == null) {
            return false;
        } else if (userService.selectIdByToken(userToken) != null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return
     * @Author chendanping
     * @Description //TODO 用户登录
     * @Date 21:21 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultBody login(@RequestBody(required = false) Map<String, String> loginInfo) {
        Integer userId = userService.selectIdByPhonenumberAndpwd(loginInfo.get("phoneNumber"), loginInfo.get("password"));

        if (userId == null) {
            return ResultBody.isError(401, "手机号码或密码有误");
        } else {
            String token = TokenUtils.token(loginInfo.get("phoneNumber"), loginInfo.get("password"));
            userService.updateTokenById(token, userId);//成功返回1

            Map data = new HashMap();
            data.put("token", token);
            data.put("userId", userId);
            data.put("username", userService.selectUsernameById(userId));

            return ResultBody.isOk(200, "登录成功", data);
        }
    }

    /**
     * @return
     * @Author chendanping
     * @Description //TODO 用户注册
     * @Date 20:59 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResultBody register(@RequestBody(required = false) Map<String, String> registerInfo) {
        Integer phonenumber = userService.selectUserIdByPhonenumber(registerInfo.get("phoneNumber"));
        if (phonenumber != null) {
            return ResultBody.isError(401, "该手机号已注册");
        } else if (userService.insertUser(
                null,
                registerInfo.get("userName"),
                registerInfo.get("password"),
                registerInfo.get("phoneNumber"),
                "pic",
                "0",
                registerInfo.get("isShoper"),
                "",
                null) == 1) {
            return ResultBody.isOk(200, "注册成功");

        } else {
            return ResultBody.isError(500, "网络故障，注册失败");
        }

    }

    /**
     * @return
     * @Author chendanping
     * @Description //TODO 用户登出
     * @Date 21:21 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/sys/logout", method = RequestMethod.GET)
    public ResultBody logout(HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));

        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        }

        Integer result = userService.updateTokenByToken(request.getHeader("Authorization"));
        System.out.println(result);
        if (result == 1) {
            return ResultBody.isOk(200, "注销成功");
        } else {
            return ResultBody.isError(502, "网络原因，注销失败");
        }
    }


}
